package com.sky.service.impl;

import com.sky.service.ReportService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import org.apache.http.HttpResponse;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private WorkspaceService workspaceService;
    @Autowired
    private HttpServletResponse response;

    /**
     * 导出数据
     */
    @Override
    public void export() {
        // 查询近30天的营业数据
        // 设置两个时间参数
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime begin = LocalDateTime.of(end.toLocalDate(), LocalTime.MIN).minusDays(30);
        // 传入参数获取营业数据对象
        BusinessDataVO dataVO = workspaceService.getBusinessData(begin, end);
        // 创建POI对象
        // 用类加载器获取模板文件
        InputStream is = ReportServiceImpl.class.getClassLoader().getResourceAsStream("template.xls");
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            // 通过对象获取到模板表格的工作表
            XSSFSheet sheet = workbook.getSheetAt(0);
            // 获取到工作表之后获取概览数据的行数
            XSSFRow row1 = sheet.getRow(3);
            // 存入营业额
            row1.getCell(2).setCellValue(dataVO.getTurnover());
            // 存入订单完成率
            row1.getCell(4).setCellValue(dataVO.getOrderCompletionRate());
            // 存入新增用户数
            row1.getCell(6).setCellValue(dataVO.getNewUsers());
            XSSFRow row2 = sheet.getRow(4);
            // 存入有效订单
            row2.getCell(2).setCellValue(dataVO.getValidOrderCount());
            // 存入平均客单价
            row2.getCell(4).setCellValue(dataVO.getUnitPrice());

            // 获取明细数据
            // 要获取30天的数据，那就要循环添加30次的数据
            for (int i = 0; i < 30; i++) {
                // 在循环中设置自增的时间
                LocalDate time = LocalDate.now().minusDays(30);
                LocalDateTime beginTime = LocalDateTime.of(time, LocalTime.MIN);
                LocalDateTime endTime = LocalDateTime.of(time, LocalTime.MAX);
                // 获取当天的营业额数据对象
                BusinessDataVO dataVO1 = workspaceService.getBusinessData(beginTime.plusDays(i), endTime.plusDays(i));
                // 获取明细数据的行数
                XSSFRow row = sheet.getRow(7+i);
                // 往单元格中存入数据
                //存入日期
                row.getCell(1).setCellValue(time.plusDays(i).toString());
                //存入营业额
                row.getCell(2).setCellValue(dataVO1.getTurnover());
                //存入有效订单
                row.getCell(3).setCellValue(dataVO1.getValidOrderCount());
                //存入订单完成率
                row.getCell(4).setCellValue(dataVO1.getOrderCompletionRate());
                //存入平均客单价
                row.getCell(5).setCellValue(dataVO1.getUnitPrice());
                //存入新增用户数
                row.getCell(6).setCellValue(dataVO1.getNewUsers());
            }
            // 把数据写出到浏览器中
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            // 关闭流
            workbook.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
